package com.zhisheng.day16_echarts.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.text.DecimalFormat;
import java.util.*;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyEchartsVo {
    private String name;
    private Integer id;
    private Double averageScore;
    private Set<Map<String, Object>> scores = new TreeSet<>((m1,m2)->
        m1.get("time").toString().compareTo(m2.get("time").toString())
    );

    public MyEchartsVo(String name, Integer id) {
        this.name = name;
        this.id = id;
    }

    // 根据集合中的分数求平均分
    public Double getAverageScore() {
        double v = scores.stream().mapToDouble(map -> Double.parseDouble(map.get("score").toString())).average().getAsDouble();
        DecimalFormat df = new DecimalFormat("#.00");
        String formattedAverage = df.format(v);
        return Double.parseDouble(formattedAverage);
    }
}
